package com.css.dzj.dzjzhfx.modules.behavior;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.css.dzj.dzjzhfx.common.util.HttpClientUtil;
import com.css.dzj.dzjzhfx.common.util.R;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@PropertySource(value = "classpath:config.properties")
@RequestMapping("/havior")
public class BehaviorByYear {
    @Value("${apURL}")
    private String apURL;

    /***
     * 获取检查行为
     * @return
     */
    @GetMapping("/countyear")
    public R count(String year) {
        String[] mapPro = new String[]{"南海诸岛", "北京", "天津", "上海", "重庆", "河北", "河南", "云南", "辽宁", "黑龙江", "湖南", "安徽", "山东", "新疆", "江苏", "浙江", "江西", "湖北", "广西", "甘肃", "山西", "内蒙古", "陕西", "吉林", "福建", "贵州", "广东", "青海", "西藏", "四川", "宁夏", "海南", "台湾", "香港", "澳门"};
        /***
         * 获取检查行为
         */
        String msg = HttpClientUtil.doGet(apURL + "/check/countbyyear?year=" + year);
        JSONObject jsonObject = JSON.parseObject(msg);
        Map<String, Object> mapCheck = new HashMap<>();
        if(!StringUtils.isEmpty(msg)){
            Object count = jsonObject.get("count");
            mapCheck = JSONObject.parseObject(count.toString());
        }
        /**
         * 获取处罚行为
         */
        String msgPunish = HttpClientUtil.doGet(apURL + "/punish/punishyear?year=" + year);
        JSONObject jsonPunish = JSON.parseObject(msgPunish);
        Map<String, Object> mapPunish = new HashMap<>();
        if(!StringUtils.isEmpty(msgPunish)){
            Object punishOb = jsonPunish.get("count");
            mapPunish = JSONObject.parseObject(punishOb.toString());
        }
        /**
         * 获取强制行为
         */
        String msgForce = HttpClientUtil.doGet(apURL + "/force/forceyear?year=" + year);
        JSONObject jsonForce = JSON.parseObject(msgForce);
        Map<String, Object> mapForce = new HashMap<>();
        if(!StringUtils.isEmpty(msgForce)){
            Object forceOb = jsonForce.get("count");
            mapForce = JSONObject.parseObject(forceOb.toString());
        }
        /**
         * 获取其他行为
         */
        String msgOther = HttpClientUtil.doGet(apURL + "/other/otheryear?year=" + year);
        JSONObject jsonOther = JSON.parseObject(msgOther);
        Map<String, Object> mapOther = new HashMap<>();
        if(!StringUtils.isEmpty(msgOther)){
            Object otherOb = jsonOther.get("count");
            mapOther = JSONObject.parseObject(otherOb.toString());
        }
        List<Map<String, Object>> list = new ArrayList();
        list.add(mapCheck);
        list.add(mapPunish);
        list.add(mapForce);
        list.add(mapOther);
        Map<String, Object> mapAll = new HashMap<>();
        if(!list.isEmpty()) {
            for (Map<String, Object> map : list) {
                for (Map.Entry<String, Object> entry : map.entrySet()) {
                    String key = entry.getKey();
                    Integer value = (Integer) entry.getValue();
                    Integer valueAll = (Integer) mapAll.get(entry.getKey());
                    if (valueAll == null) {
                        mapAll.put(key, value);
                    } else {
                        valueAll = valueAll.intValue() + value.intValue();
                        mapAll.put(key, valueAll);
                    }
                }
            }
        }
        List<Map<String, Object>> listNew = new ArrayList();
        Integer num = 0;
        for (Map.Entry<String, Object> entry : mapAll.entrySet()) {
            HashMap<String, Object> map = new HashMap();
            String name = entry.getKey();
            Integer value = (Integer) entry.getValue();
            num += value;
            map.put("name", name);
            map.put("value", value);
            listNew.add(map);
        }
        for(String arr:mapPro){
            HashMap mapNew=new HashMap();
            for (Map map:listNew) {
                String str=(String)map.get("name");
                if(!arr.equals(str)){
                    mapNew.put("name",arr);
                    mapNew.put("value",0);
                }
            }
            listNew.add(mapNew);
        }
        String data = JSON.toJSONString(listNew);
        return R.ok().put("data", data).put("cityTotal", num);
    }

    /***
     * 获取饼状图行为
     * @return
     */
    @GetMapping("/pieyear")
    public R getPunish(String year) {
        /***
         * 获取检查行为
         */
        String msgCheck = HttpClientUtil.doGet(apURL + "/check/countbyyear?year=" + year);
        JSONObject jsonObject = JSON.parseObject(msgCheck);
        Map<String, Object> mapCheck = new HashMap<>();
        List list = new ArrayList();
        String dataCheck="";
        if(!StringUtils.isEmpty(msgCheck)){
            Object count = jsonObject.get("count");
            mapCheck = JSONObject.parseObject(count.toString());
            List listCheck = new ArrayList();
            List<String> check = new ArrayList<>();
            for (Map.Entry<String, Object> entry : mapCheck.entrySet()) {
                HashMap map = new HashMap();
                String name = entry.getKey();
                Integer value = (Integer) entry.getValue();
                map.put("name", name);
                map.put("value", value);
                listCheck.add(map);
                check.add(name);
            }
            dataCheck = JSON.toJSONString(listCheck);
            for (String chk : check) {
                list.add(chk);
            }
        }
        /**
         * 获取处罚行为
         */
        String msgPunish = HttpClientUtil.doGet(apURL + "/punish/punishyear?year=" + year);
        JSONObject jsonPunish = JSON.parseObject(msgPunish);
        Map<String, Object> mapPunish = new HashMap<>();
        String dataPunish="";
        if(!StringUtils.isEmpty(msgPunish)){
            Object punishOb = jsonPunish.get("count");
            mapPunish = JSONObject.parseObject(punishOb.toString());
            List listPunish = new ArrayList();
            List<String> punish = new ArrayList<>();
            for (Map.Entry<String, Object> entry : mapPunish.entrySet()) {
                HashMap map = new HashMap();
                String name = entry.getKey();
                Integer value = (Integer) entry.getValue();
                map.put("name", name);
                map.put("value", value);
                listPunish.add(map);
                punish.add(name);
            }
                dataPunish = JSON.toJSONString(listPunish);
            for (String pun : punish) {
                if (!list.contains(pun)) {
                    list.add(pun);
                }

            }
        }
        /**
         * 获取强制行为
         */
        String msgForce = HttpClientUtil.doGet(apURL + "/force/forceyear?year=" + year);
        JSONObject jsonForce = JSON.parseObject(msgForce);
        Map<String, Object> mapForce = new HashMap<>();
        String dataForce="";
        if(!StringUtils.isEmpty(msgForce)){
            Object forceOb = jsonForce.get("count");
            mapForce = JSONObject.parseObject(forceOb.toString());
            List listForce = new ArrayList();
            List<String> force = new ArrayList<>();
            for (Map.Entry<String, Object> entry : mapForce.entrySet()) {
                HashMap map = new HashMap();
                String name = entry.getKey();
                Integer value = (Integer) entry.getValue();
                map.put("name", name);
                map.put("value", value);
                listForce.add(map);
                force.add(name);
            }
             dataForce = JSON.toJSONString(listForce);
            for (String forc : force) {
                if (!list.contains(forc)) {
                    list.add(force);
                }
            }
        }
        /**
         * 获取其他行为
         */
        String msgOther = HttpClientUtil.doGet(apURL + "/other/otheryear?year=" + year);
        JSONObject jsonOther = JSON.parseObject(msgOther);
        Map<String, Object> mapOther = new HashMap<>();
        String dataOther="";
        if(!StringUtils.isEmpty(msgOther)){
            Object otherOb = jsonOther.get("count");
            mapOther = JSONObject.parseObject(otherOb.toString());
            List listOther = new ArrayList();
            List<String> other = new ArrayList<>();
            for (Map.Entry<String, Object> entry : mapOther.entrySet()) {
                HashMap map = new HashMap();
                String name = entry.getKey();
                Integer value = (Integer) entry.getValue();
                map.put("name", name);
                map.put("value", value);
                listOther.add(map);
                other.add(name);
            }
             dataOther = JSON.toJSONString(listOther);
            for (String oth : other) {
                if (!list.contains(oth)) {
                    list.add(oth);
                }
            }
        }
        //List list = new ArrayList();

        return R.ok().put("dataCheck", dataCheck).put("dataPunish", dataPunish).
                put("dataForce", dataForce).put("dataOther", dataOther).put("name", list);
    }

    /***
     * 柱状图
     * @return
     */
    @GetMapping("/getbaryear")
    public R getBar(String year) {
        /***
         * 获取检查行为
         */
        String msgCheck = HttpClientUtil.doGet(apURL + "/check/countbyyear?year=" + year);
        JSONObject jsonObject = JSON.parseObject(msgCheck);
        Map<String, Object> mapCheck = new HashMap<>();
        if(!StringUtils.isEmpty(msgCheck)){
            Object count = jsonObject.get("count");
            mapCheck = JSONObject.parseObject(count.toString());
        }

        List listCheck = new ArrayList();
        List<String> check = new ArrayList<>();
        /**
         * 获取处罚行为
         */
        String msgPunish = HttpClientUtil.doGet(apURL + "/punish/punishyear?year=" + year);
        JSONObject jsonPunish = JSON.parseObject(msgPunish);
        Map<String, Object> mapPunish = new HashMap<>();
        if(!StringUtils.isEmpty(msgPunish)){
            Object punishOb = jsonPunish.get("count");
            mapPunish = JSONObject.parseObject(punishOb.toString());
        }

        List<String> punish = new ArrayList<>();
        List listPunish = new ArrayList();
        /**
         * 获取强制行为
         */
        String msgForce = HttpClientUtil.doGet(apURL + "/force/forceyear?year=" + year);
        JSONObject jsonForce = JSON.parseObject(msgForce);
        Map<String, Object> mapForce = new HashMap<>();
        if(!StringUtils.isEmpty(msgForce)){
            Object forceOb = jsonForce.get("count");
            mapForce = JSONObject.parseObject(forceOb.toString());
        }
        List listForce = new ArrayList();
        List<String> force = new ArrayList<>();

        /**
         * 获取其他行为
         */
        String msgOther = HttpClientUtil.doGet(apURL + "/other/otheryear?year=" + year);
        JSONObject jsonOther = JSON.parseObject(msgOther);
        Map<String, Object> mapOther = new HashMap<>();
        if(!StringUtils.isEmpty(msgOther)){
            Object otherOb = jsonOther.get("count");
            mapOther = JSONObject.parseObject(otherOb.toString());
        }
        List listOther = new ArrayList();
        List<String> other = new ArrayList();
        /***
         * 获取检查行为的机构
         */
        for (Map.Entry<String, Object> entry : mapCheck.entrySet()) {
            String name = entry.getKey();
            check.add(name);
        }
        /***
         * 获取处罚行为的机构
         */
        for (Map.Entry<String, Object> entry : mapPunish.entrySet()) {
            String name = entry.getKey();
            punish.add(name);
        }
        /**
         * 获取强制行为的机构
         */
        for (Map.Entry<String, Object> entry : mapForce.entrySet()) {
            String name = entry.getKey();
            force.add(name);
        }
        /***
         * 获取其他行为的机构
         */
        for (Map.Entry<String, Object> entry : mapOther.entrySet()) {
            String name = entry.getKey();
            other.add(name);
        }
        for (String chk : check) {
            if (!punish.contains(chk)) {
                mapPunish.put(chk, 0);
            }
            if (!force.contains(chk)) {
                mapForce.put(chk, 0);
            }
            if (!other.contains(chk)) {
                mapOther.put(chk, 0);
            }
        }
        for (String pun : punish) {
            if (!check.contains(pun)) {
                mapCheck.put(pun, 0);
            }
            if (!force.contains(pun)) {
                mapForce.put(pun, 0);
            }
            if (!other.contains(pun)) {
                mapOther.put(pun, 0);
            }
        }
        for (String fore : force) {
            if (!check.contains(fore)) {
                mapCheck.put(fore, 0);
            }
            if (!punish.contains(fore)) {
                mapPunish.put(fore, 0);
            }
            if (!other.contains(fore)) {
                mapOther.put(fore, 0);
            }
        }
        for (String oth : other) {
            if (!check.contains(oth)) {
                mapCheck.put(oth, 0);
            }
            if (!force.contains(oth)) {
                mapForce.put(oth, 0);
            }
            if (!punish.contains(oth)) {
                mapPunish.put(oth, 0);
            }
        }
        for (Map.Entry<String, Object> entry : mapCheck.entrySet()) {
            HashMap map = new HashMap();
            String name = entry.getKey();
            Integer value = (Integer) entry.getValue();
            map.put("name", name);
            map.put("value", value);
            listCheck.add(map);
        }
        String dataCheck = JSON.toJSONString(listCheck);

        for (Map.Entry<String, Object> entry : mapPunish.entrySet()) {
            HashMap map = new HashMap();
            String name = entry.getKey();
            Integer value = (Integer) entry.getValue();
            map.put("name", name);
            map.put("value", value);
            listPunish.add(map);
        }
        String dataPunish = JSON.toJSONString(listPunish);

        for (Map.Entry<String, Object> entry : mapForce.entrySet()) {
            HashMap map = new HashMap();
            String name = entry.getKey();
            Integer value = (Integer) entry.getValue();
            map.put("name", name);
            map.put("value", value);
            listForce.add(map);
        }
        String dataForce = JSON.toJSONString(listForce);

        for (Map.Entry<String, Object> entry : mapOther.entrySet()) {
            HashMap map = new HashMap();
            String name = entry.getKey();
            Integer value = (Integer) entry.getValue();
            map.put("name", name);
            map.put("value", value);
            listOther.add(map);
        }
        String dataOther = JSON.toJSONString(listOther);
        /*List list=new ArrayList();
        for(String chk:check){
            list.add(chk);
        }
        for(String pun:punish){
            if(!list.contains(pun)){
                list.add(pun);
            };
        }
        for (String forc:force){
            if(!list.contains(forc)){
                list.add(force);
            }
        }

        for(String oth:other){
            if(!list.contains(oth)){
                list.add(oth);
            }
        }*/
        return R.ok().put("dataCheck", dataCheck).put("dataPunish", dataPunish).
                put("dataForce", dataForce).put("dataOther", dataOther);
    }
}
